/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.domain.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.StatBus;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.domain.DomainMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class BusMonitorContent extends Content {
	private DomainMessages cons;
	private BifangGWTServiceAsync service;
	private String busCode = null;
	private VerticalPanel p;
	private GwtContext context;
	private Grid table1 = null;
	
	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());

		}

		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}
			StatBus b = (StatBus)res.getResponse();
			table1.setHTML(0, 1, b.getStatBeginTime());
			table1.setHTML(1, 1, String.valueOf(b.getTotalRequest()));
			table1.setHTML(2, 1, String.valueOf(b.getSuccessRequest()));
			table1.setHTML(3, 1, String.valueOf(b.getTotalRequest() - b.getSuccessRequest()));
			if(b.getTotalRequest() != 0)
			table1.setHTML(4, 1, String.valueOf(Double.valueOf(b.getSuccessRequest()) / b.getTotalRequest() * 100) + "%");
			else
				table1.setHTML(4, 1, "N/A");
				

		}
	};


	public BusMonitorContent(DomainMessages cons,
			BifangGWTServiceAsync service1, String busCode1, GwtContext context) {
		this.cons = cons;
		this.service = service1;
		this.busCode = busCode1;
		this.context = context;

		p = new VerticalPanel();
		p.setWidth("100%");
		p.setBorderWidth(0);
		p.setSpacing(10);

		p.add(new HTML(cons.stat()));

		table1 = new Grid(5, 2);
		table1.setWidth("70%");
		ColumnFormatter cf = table1.getColumnFormatter();
		cf.setWidth(0, "20%");
		cf.setWidth(1, "60%");

		table1.setCellPadding(1);
		table1.setCellSpacing(1);
		table1.setBorderWidth(1);

		CellFormatter ff = table1.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		ff.setStyleName(3, 0, "tablecol");
		ff.setStyleName(4, 0, "tablecol");
		
		table1.setHTML(0, 0, cons.statBegin());
		table1.setHTML(1, 0, cons.totalRequest());
		table1.setHTML(2, 0, cons.successRequest());
		table1.setHTML(3, 0, cons.failRequest());
		table1.setHTML(4, 0, cons.successRatio());
		
		p.add(table1);
		
		Button b = new Button(cons.refresh());
		b.addClickListener(new ClickListener(){

			
			public void onClick(Widget sender) {
				RequestObject req = new RequestObject();
				req.setRequest(busCode);
				req.setAction("han.bifang.gwt.server.action.domain.GetBusStatAction");
				
				service.call(req, cb);
				
			}});
		p.add(b);
		initWidget(p);
	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req.setRequest(busCode);
		req.setAction("han.bifang.gwt.server.action.domain.GetBusStatAction");
		
		service.call(req, cb);
	}

}
